package com.yxhl.zoume.core.busticket.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.yxhl.protobuf.OneYxBusInfoRequest;
import com.yxhl.protobuf.ScheduleType;
import com.yxhl.zoume.R;
import com.yxhl.zoume.common.ui.activity.base.BaseActivityWithToolBar;
import com.yxhl.zoume.core.busticket.ui.fragment.BusTicketOrderFragment;
import com.yxhl.zoume.core.func.webpage.info.WebPageEntrance;
import com.yxhl.zoume.di.component.base.HasComponent;
import com.yxhl.zoume.di.component.busticket.BusServiceComponent;
import com.yxhl.zoume.di.component.busticket.DaggerBusServiceComponent;
import com.yxhl.zoume.di.module.busticket.BusServiceModule;

public class BusTicketOrderActivity extends BaseActivityWithToolBar implements HasComponent<BusServiceComponent> {

    private static final String INTENT_EXTRA_PARAM_TICKET_DETAIL = "INTENT_EXTRA_PARAM_TICKET_DETAIL";
    private static final String INTENT_EXTRA_PARAM_SCHEDULE_TYPE = "INTENT_EXTRA_PARAM_SCHEDULE_TYPE";

    BusServiceComponent component;
    OneYxBusInfoRequest mOneYxBusInfoRequest;
    ScheduleType mScheduleType;

    /**
     * intent for navigation
     *
     * @param context
     * @param param
     * @return
     */
//    public static Intent getCallingIntent(Context context, OneYxBusInfoRequest param) {
//        Intent callingIntent = new Intent(context, BusTicketOrderActivity.class);
//        callingIntent.putExtra(INTENT_EXTRA_PARAM_TICKET_DETAIL, param);
//        return callingIntent;
//    }
    public static Intent getCallingIntent(Context context, OneYxBusInfoRequest param, ScheduleType scheduleType) {
        Intent callingIntent = new Intent(context, BusTicketOrderActivity.class);
        callingIntent.putExtra(INTENT_EXTRA_PARAM_TICKET_DETAIL, param);
        callingIntent.putExtra(INTENT_EXTRA_PARAM_SCHEDULE_TYPE, scheduleType);
        return callingIntent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        handleIntent();

        addFragment(R.id.bus_ticket_order_container, BusTicketOrderFragment.newInstance(mOneYxBusInfoRequest, mScheduleType));
    }

    private void handleIntent() {
        Intent intent = getIntent();
        if (null != intent) {
            Bundle bundle = intent.getExtras();
            mOneYxBusInfoRequest = (OneYxBusInfoRequest) bundle.getSerializable(INTENT_EXTRA_PARAM_TICKET_DETAIL);
            mScheduleType = (ScheduleType) bundle.getSerializable(INTENT_EXTRA_PARAM_SCHEDULE_TYPE);
        }
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_bus_ticket_order;
    }

    @Override
    protected void initializeInjector() {
        this.component = DaggerBusServiceComponent.builder()
                .appComponent(getAppComponent())
                .activityModule(getActivityModule())
                .busServiceModule(new BusServiceModule())
                .build();
    }

    @Override
    protected void onHeaderLeftClick(View view) {
        onBackPressed();
    }

    @Override
    protected void onHeaderRightClick(View view) {
        if (mScheduleType != null) {
            switch (mScheduleType) {
                case V_FT_BUS:
                    getAppComponent().navigator().navigateToWebPageActivity(this, WebPageEntrance.FIXED_BUS_INTRODUCTION);
                    break;
                case V_XD_BUS:
                case V_DABA_BUS:
                    getAppComponent().navigator().navigateToWebPageActivity(this, WebPageEntrance.TICKET_BUS_INTRODUCTION);
                    break;
            }
        } else {
            getAppComponent().navigator().navigateToWebPageActivity(this, WebPageEntrance.TICKET_BUS_INTRODUCTION);
        }
    }

    @Override
    public BusServiceComponent getComponent() {
        return component;
    }
}
